import { _decorator, Component, easing, Label, Node, tween, Vec3 } from 'cc';
import { GAME_MODE, GAMEDATA } from './GAMEDATA';
import { Global } from '../Global';
import { GameScene } from './GameScene';
import { LocalStorageMgr } from '../Mgr/LocalStorageMgr';
import { Prompt_Cube } from './Prompt_Cube';
const { ccclass, property } = _decorator;

@ccclass('ModelLoad')
/**
 * 模式层
 */
export class ModelLoad extends Component {
    //游戏步数
    @property(Label)
    private Step: Label = null;
    public StepNum: number = 0;

    //游戏时间
    @property(Label)
    private Time: Label = null;
    private TimeNum: number = 0;

    //游戏模型数量
    @property(Node)
    private CubeContent: Node = null;
    private CubeArray: Node[] = [];

    //分数
    @property(Label)
    private Score: Label = null;
    public ScoreNum: number = 0;

    //
    @property(Label)
    private BestScore: Label = null;

    //提示方块层
    @property(Node)
    private PromptCube: Node = null;
    //提示数量
    private PromptNum: number = 0;

    protected start(): void {
        this.UpdateBestScore();
    }

    /**
     * 更新最高分数
     */
    public UpdateBestScore() {
        switch (GAMEDATA.GAME_MODE) {
            case GAME_MODE.NORMAL:
                this.BestScore.string = LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.NORMAL_SCORE.toString();
                break;
            case GAME_MODE.CHANGE:
                this.BestScore.string = LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.CHANGE_SCORE.toString();
                break;
            case GAME_MODE.TIME:
                this.BestScore.string = LocalStorageMgr.Instance.LOCAL_STORAGE_DATA.TIME_SCORE.toString();
                break;
        }
    }


    /**
     * 更新游戏模式
     */
    public UpdateModel() {
        this.Time.node.active = false;
        this.Step.node.active = false;
        this.CubeContent.active = false;

        this.ScoreNum = 0;
        this.StepNum = GAMEDATA.GAME_STEP;
        this.UpdateScore(0);


        if (GAMEDATA.GAME_MODE == GAME_MODE.CHANGE) {
            this.Step.node.active = true;
            this.StepNum = GAMEDATA.GAME_STEP;
            this.UpdateStep(0);
            this.UpdatePromptCube();
        }

        if (GAMEDATA.GAME_MODE == GAME_MODE.TIME) {
            this.Time.node.active = true;
            this.TimeNum = GAMEDATA.GAME_TIME;
            this.UpdateTime();
        }

    }

    /**
     * 更新游戏步数
     */
    public UpdateStep(_step: number) {
        if (GAMEDATA.GAME_MODE != GAME_MODE.CHANGE) return;//如果不是挑战模式返回
        this.StepNum += _step;

        if (this.StepNum <= 0) {
            this.StepNum = 0;
        }

        this.Step.string = "b" + this.StepNum.toString();
    }

    /**
     * 更新游戏分数
     */
    public UpdateScore(_score: number) {
        this.ScoreNum += _score;
        this.Score.string = "f" + this.ScoreNum.toString();
    }

    /**
     * 更新游戏时间
     */
    public UpdateTime() {
        this.Time.string = "t" + Math.floor(this.TimeNum).toString();
    }

    protected update(dt: number): void {
        if (Global.IsEnd) return;
        if (GAMEDATA.GAME_MODE == GAME_MODE.TIME) {
            this.TimeNum -= dt;

            if (this.TimeNum <= 0) {
                this.TimeNum = 0;
                GameScene.Instance.GameOver();
            }

            this.UpdateTime();
        }
    }

    /**
     * 更新提示方块
     */
    public UpdatePromptCube() {
        this.PromptCube.active = true;
        this.PromptNum = 1 + Math.floor(Math.random() * (Global.CubeStyle - 3));
        let cube_style: number[] = [0, 1, 2, 3, 4];

        for (let pc of this.PromptCube.children) pc.active = false;

        for (let i = 0; i < this.PromptNum; i++) {
            this.PromptCube.children[i].active = true;
            let ran_num: number = Math.floor(Math.random() * cube_style.length);
            this.PromptCube.children[i].getComponent(Prompt_Cube).UpdateCubeInfor(
                cube_style[ran_num],
                3 + Math.floor(Math.random() * 10)
            );
            cube_style.splice(ran_num, 1);
        }


    }

    /**
     * 放置成功更新提示方块
     */
    public ClearSuccessUpdatePromptCube(_cubeindex: number, _num: number) {
        if (GAMEDATA.GAME_MODE != GAME_MODE.CHANGE) return;

        for (let pc of this.PromptCube.children) {
            pc.getComponent(Prompt_Cube).RemoveCube(_cubeindex, _num)
        }

        //判断是否都消失了
        for (let pc of this.PromptCube.children) {
            if (pc.active && pc.getComponent(Prompt_Cube).CubeNum > 0) {
                return;
            }
        }

        this.UpdateStep(this.PromptNum + 3);

        this.UpdatePromptCube();

    }
}


